//
//  QDUnionFillViewController.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/5.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 银联信息填写
final class QDUnionFillViewController: NWTableViewController,QDLargeTitleTableHeaderScrollable {

    /// 返回的控制器
    weak var backViewController: UIViewController?
    /// 大标题
    let largeTableHeaderView =  QDLargeTitleTableHeaderView()
    /// 填写信息的headerView
    let fillHeaderView = QDUnionFillHeaderView()
    /// 下一步
    let footerView = QDUnionFillFooterView()
    
    override func loadPropertysAtInitialization() {
        super.loadPropertysAtInitialization()
        
        headerRefreshEnable = false
        footerRefreshEnable = false
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        largeTableHeaderView.title = "银联信息填写"
        fillHeaderView.ritl_size = CGSize(width: ritl_width, height: 286)
        footerView.ritl_size = CGSize(width: ritl_width, height: 44 + 25)
        
        footerView.nextButton.addTarget(self, action: #selector(gotoSign), for: .touchUpInside)
        
        let view = UIView()
        view.ritl_size = CGSize(width: ritl_width, height: QDLargeTitleTableHeaderViewNormalHeight + 286)
        view.addSubview(largeTableHeaderView)
        view.addSubview(fillHeaderView)
        
        largeTableHeaderView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(QDLargeTitleTableHeaderViewNormalHeight)
        }
        
        fillHeaderView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(largeTableHeaderView.snp.bottom)
        }
        
        tableView.tableHeaderView = view
        tableView.tableFooterView = footerView
        
    }
    
    // MARK: NavigationBar
    override var nav_mutableBarShadowHidden: Bool { return true }
    
    
    // MARK: ScollView
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        tableViewDidScroll(contentOfset: scrollView.contentOffset) { (isShow) in
            setNavigationBarShadowViewHidden(!isShow)
        }
    }
    
    
    /// 前去签约
    @objc func gotoSign(){
        
        view.endEditing(true)
        
        // 获得内容
        let contents = fillHeaderView.texts
        // 参数
        var parameters = [String: String]()
        //进行甄别
        for (_,(key,content)) in contents.enumerated() {
            
            /// 姓名
            if key == "name" {
                if !ritl_checkStringProperty(content) {
                    view.showTextHUD(text: "请输入正确的姓名",delayHidden: 0.5); return
                }
                 parameters["realName"] = content
            }
            
            /// 身份证
            else if key == "id" {
                if !QDConstant.validateIdentityCard(content) {
                    view.showTextHUD(text: "请输入正确的身份证号",delayHidden: 0.5); return
                }
                 parameters["idCode"] = content
            }
            
            
            /// 银行卡号
            else if key == "bank" {
                if !ritl_checkStringProperty(content) {
                    view.showTextHUD(text: "请输入正确的银行卡号",delayHidden: 0.5); return
                }
                parameters["cardNum"] = content
            }
            
            
            else if key == "phone" {
                if !ritl_checkStringProperty(content) && !RITLFilter.phone(content) {
                    view.showTextHUD(text: "请输入正确的手机号",delayHidden: 0.5); return
                }
                 parameters["phoneNum"] = content
            }
        }
        
        parameters["payChannel"] =  QDPaySignType.Union.rawValue
        
        //进行签约
        QDPaymentSignManager.sharedInstance.sign(type: .Union, data: parameters, delegate: self, viewController: self)
    }
}


extension QDUnionFillViewController: QDPaymentSignManagerDelegate {
    
    // 成功
    func complete(payType: QDPayType, data: String,id: String){
        
        //只能是银联
        if payType != .Union { return }
        
        //启动银联网页签约
        let viewController = QDUnionSignViewController()
        viewController.message = data
        viewController.orderId = id
        viewController.backViewController = backViewController
        
        ritl_topNavigationPush(viewController, animated: true)
        
    }
    
    
    func fail(error: String){
        view.showTextHUD(text:error)
    }
    
}
